﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace WGM_Client_Net35V2
{
    /// <summary>
    /// Interaction logic for PopupWindow.xaml
    /// </summary>
    public partial class PopupWindow : Window
    {
        IMainWindow _mainWindow;
        int _remainingDisplayTime = 0;
        int _lastRequestedDisplayTime = 0;
        DispatcherTimer _displayTimer = new DispatcherTimer();
        ContactObject _destination;

        public PopupWindow(IMainWindow mainWindow)
        {
            InitializeComponent();
            _mainWindow = mainWindow;
            _displayTimer.Interval = TimeSpan.FromSeconds(1);
            _displayTimer.Tick += new EventHandler(_displayTimer_Tick);
            this.ShowActivated = false;
            this.Focusable = false;
            this.Topmost = true;
            
        }

        void _displayTimer_Tick(object sender, EventArgs e)
        {
            if (this.IsMouseOver || _rootGrid.IsMouseOver || _title.IsMouseOver || _messageBody.IsMouseOver || _closeButton.IsMouseOver)
                _remainingDisplayTime = _lastRequestedDisplayTime;
            else
                _remainingDisplayTime -= 1;
            if (_remainingDisplayTime < 0)
            {
                _displayTimer.Stop();
                FadeOut();
            }
        }
        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            _displayTimer.Stop();
            FadeOut();
        }
        public void ShowModal(string message, string title, FrameworkElement image)
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            _messageBody.Text = message;
            _messageBody.ToolTip = _messageBody.Text;
            _title.Text = title;
            _imageContent.Content = image;
            this.ShowDialog();
        }
        public void ShowWindow(int duration, string title, string message, ContactObject sender)
        {
            _remainingDisplayTime = _lastRequestedDisplayTime = duration;
            Border contactImageBorder = new Border();
            contactImageBorder.CornerRadius = new CornerRadius(5);
            contactImageBorder.Background = new ImageBrush((sender.ContactDisplayImage.Background as ImageBrush).ImageSource);
            _imageContent.Content = contactImageBorder;

            _destination = sender;
            _title.Text = title;
            _messageBody.Text = message;
            _messageBody.ToolTip = _messageBody.Text;
            int topLoc = (SystemInformation.PrimaryMonitorMaximizedWindowSize.Height - (int)this.Height) - 10;
            int leftLoc = (SystemInformation.PrimaryMonitorSize.Width - (int)this.Width) - 5;
            this.Left = leftLoc;
            this.Top = topLoc;
            if (!this.IsVisible)
                FadeIn();
            else
            {
                this.Show();
            }
            _displayTimer.Start();
        }
        public void ShowWindow(int duration, WGM_Svc.WGMMessage message,ContactObject sender, ContactObject destination)
        {
            _remainingDisplayTime = _lastRequestedDisplayTime = duration;

            Border contactImageBorder = new Border();
            contactImageBorder.CornerRadius = new CornerRadius(5);
            contactImageBorder.Background = new ImageBrush((sender.ContactDisplayImage.Background as ImageBrush).ImageSource);
            _imageContent.Content = contactImageBorder;
            
            _destination = destination;
            switch (message.MessageType)
            {
                case WGM_Client_Net35V2.WGM_Svc.MessageTypes.GLOBAL_MESSAGE:
                    _title.Text = "New Global Message";
                    _messageBody.Text = sender.ContactDisplayName + " says:\r\n" + message.Message as string;
                    break;
                case WGM_Client_Net35V2.WGM_Svc.MessageTypes.PRIVATE_MESSAGE:
                    _title.Text = "New Private Message";
                    _messageBody.Text = sender.ContactDisplayName + " says:\r\n" + message.Message as string;
                    break;
            }
            _messageBody.ToolTip = _messageBody.Text;
            int topLoc = (SystemInformation.PrimaryMonitorMaximizedWindowSize.Height - (int)this.Height) - 10;
            int leftLoc = (SystemInformation.PrimaryMonitorSize.Width - (int)this.Width)-5;
            this.Left = leftLoc;
            this.Top = topLoc;
            if (!this.IsVisible)
                FadeIn();
            else
            {
                this.Show();
            }
            _displayTimer.Start();
        }
        private void FadeIn()
        {
            this.Show();
        }
        private void FadeOut()
        {
            this.Close();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_destination != null)
            {
                _mainWindow.ShowAndSelect(_destination);
                _remainingDisplayTime = 0;
                this.Close();
            }
        }
    }
}
